package com.cn.bulu.controller;

import com.cn.bulu.pojo.Customer;
import com.cn.bulu.pojo.Customer_address;
import com.cn.bulu.pojo.Dictionary;
import com.cn.bulu.pojo.Evaluation;
import com.cn.bulu.service.CustomerService;
import com.cn.bulu.utils.BuluResult;
import com.cn.bulu.utils.qrcode.QRCodeUtil;
import com.cn.bulu.utils.redis.JedisPoolUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.Jedis;
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.OutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/customer")
public class CustomerController {
	@Resource
	private CustomerService customerService;
	private static Logger logger = Logger.getLogger(CustomerController.class);

	//鹿客家居顾客注册
	@RequestMapping("/register")
	@ResponseBody
	public BuluResult register(String nickName,
			/* String phone, String password */String account, String referrer, String gender) {
		BuluResult buluResult = customerService.register(nickName, account, referrer, gender);
		
		return buluResult;
	}
	

	// 判断手机号是否已经注册
	@RequestMapping("/isPhone")
	@ResponseBody
	public BuluResult isPhone(String phone) {
		BuluResult buluResult = customerService.isPhone(phone);
		return buluResult;
	}
	
	//得到登陆用户的二维码信息
//	public BuluResult getQRCode(HttpServletRequest request){
//		BuluResult buluResult = new BuluResult();
//		customerService.getQRCode(request);
//		
//		return buluResult;
//	}
//	

	// 登录
	@RequestMapping("/login")
	@ResponseBody
	public BuluResult login(/* String phone, String password */String account, HttpServletRequest request) {
		BuluResult buluResult = customerService.login(account, request);
		return buluResult;
	}

	// 顾客账户退出(注销)
	@RequestMapping("/cancel")
	@ResponseBody
	public BuluResult cancel(HttpServletRequest request) {
		Jedis redis = JedisPoolUtils.getJedis();// 连接redis
		String sign = request.getParameter("sign");
		redis.del(sign);
		JedisPoolUtils.close(redis);
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		return buluResult;
	}

	// 修改头像
	@RequestMapping("updateHeadImg")
	@ResponseBody
	public BuluResult updateHeadImg(HttpServletRequest request, String headImg) {
		BuluResult buluResult = customerService.updateHeadImg(request, headImg);
		return buluResult;
	}

	//
	@RequestMapping("/updateUserInfo")
	@ResponseBody
	public BuluResult updateUserInfo(HttpServletRequest request) {
		BuluResult buluResult = customerService.updateUserInfo(request);
		return buluResult;
	}

	// 忘记密码
	@RequestMapping("/updatePassword")
	@ResponseBody
	public BuluResult updatePassword(String account, HttpServletRequest request) {
		BuluResult buluResult = customerService.updatePassword(account);
		return buluResult;
	}

	// 安装房间位置获取
	@RequestMapping("/getLocation")
	@ResponseBody
	public BuluResult getLocation() {
		BuluResult buluResult = customerService.getLocation();
		return buluResult;
	}

	// 点击加入购物车 goodsList:[{goods:{},accs:[{},{}]},{}]
	@RequestMapping("/addCar")
	@ResponseBody
	public BuluResult addCar(HttpServletRequest request, String goodsList) {
		BuluResult buluResult = customerService.addCar(request, goodsList);
		return buluResult;
	}

	// 点击购物车(展示所有购物车)
	@RequestMapping("/showCar")
	@ResponseBody
	public BuluResult showCar(HttpServletRequest request) {
		BuluResult buluResult = customerService.showCar(request);
		return buluResult;
	}

	// 删除购物车 goodsIds[商品id数组]
	@RequestMapping("deleteCar")
	@ResponseBody //
	public BuluResult deleteCar(String goodsIds) {
		BuluResult buluResult = customerService.deleteCar(goodsIds);
		return buluResult;
	}

	// 购物车中增加配件(对应所对应的商品)
	// 购物车(进行中)中增加配件
	@RequestMapping("/addGoodsOfAcc")
	@ResponseBody
	public BuluResult addGoodsOfAcc(HttpServletRequest request, Integer sampleId, Integer indentId, Integer goodsId,
			Integer accId, Integer whatStructure) {
		BuluResult buluResult = customerService.addGoodsOfAcc(request, sampleId, indentId, goodsId, accId,
				whatStructure);
		return buluResult;
	}

	// 把购物车里面生成订单(全是配件)
	@RequestMapping("/insertOrderOfNoIndent") // goods
	@ResponseBody // goods={goodsIds:[{},{}],cuAdId:,couponsId,isInstall:0不安装,1安装}
	public BuluResult insertOrderOfNoIndent(HttpServletRequest request, String goods) {
		BuluResult buluResult = customerService.insertOrderOfNoIndent(request, goods);
		return buluResult;
	}

	// 上面这个接口 交易没成功 顾客点击了返回 需删除订单 还原执行上面那个接口时候的数据
	@RequestMapping("/callbackOrder")
	@ResponseBody
	public BuluResult callbackOrder(HttpServletRequest request, Integer orderId) {
		BuluResult buluResult = customerService.callbackOrder(request, orderId);
		return buluResult;
	}

	// 全是配件生成订单, 交钱使用优惠卷,选择是否(这个方法暂时用不上!)
	@RequestMapping("/updateOrderPriceByUseCoupons")
	@ResponseBody
	public BuluResult updateOrderPriceByUseCoupons(HttpServletRequest request, Integer orderId, Integer couponsId) {
		BuluResult buluResult = customerService.updateOrderPriceByUseCoupons(request, orderId, couponsId);
		return buluResult;
	}

	// 生成测量单
	@RequestMapping("/insertIndent") //// goods={goodsAndAcc:[{goodsId:,accs:[accId:,]},],cuAdId:,subscribeTime:预约测量是时间}
	@ResponseBody
	public BuluResult insertIndent(HttpServletRequest request, String goods) {
		BuluResult buluResult = null;
		buluResult = customerService.insertIndent(request, goods);
		return buluResult;
	}

	// 未支付点击返回
	@RequestMapping("/deleteIndentG")
	@ResponseBody
	public BuluResult deleteIndentG(Integer indentId) {
		BuluResult buluResult = customerService.deleteIndentG(indentId);
		return buluResult;
	}

	// 点击加入收藏
	@RequestMapping("/addCollect")
	@ResponseBody
	public BuluResult addCollect(HttpServletRequest request, String sampleIdS) {
		BuluResult buluResult = customerService.addCollect(request, sampleIdS);
		return buluResult;
	}

	// 点击展示收藏
	@RequestMapping("/selectCollect")
	@ResponseBody
	public BuluResult selectCollect(HttpServletRequest request) {
		BuluResult buluResult = customerService.selectCollect(request);
		return buluResult;
	}

	// 点击删除收藏
	@RequestMapping("/deleteCollect")
	@ResponseBody
	public BuluResult deleteCollect(HttpServletRequest request, String sampleIdS) {
		BuluResult buluResult = customerService.deleteCollect(request, sampleIdS);
		return buluResult;
	}

	// 展示所有收货地址
	@RequestMapping("/showAllAddress")
	@ResponseBody
	public BuluResult showAllAddress(HttpServletRequest request) {
		BuluResult buluResult = customerService.showAllAddress(request);
		return buluResult;
	}

	// 新增收货地址
	@RequestMapping("/addNewAddress")
	@ResponseBody // address name phone
	public BuluResult addNewAddress(HttpServletRequest request, Customer_address customer_address) {
		BuluResult buluResult = customerService.addNewAddress(request, customer_address);
		return buluResult;
	}

	// 删除地址
	@RequestMapping("/deleteAddress")
	@ResponseBody
	public BuluResult deleteAddress(HttpServletRequest request, Integer cuAdId) {
		BuluResult buluResult = customerService.deleteAddress(request, cuAdId);
		return buluResult;
	}

	// 更改默认地址
	@RequestMapping("/updateDefaultAddress")
	@ResponseBody
	public BuluResult updateDefaultAddress(HttpServletRequest request, Integer cuAdId) {
		BuluResult buluResult = customerService.updateDefaultAddress(request, cuAdId);
		return buluResult;
	}

	// 展示所有优惠卷
	@RequestMapping("/showCoupons")
	@ResponseBody
	public BuluResult showCoupons(HttpServletRequest request) {
		BuluResult buluResult = customerService.showCoupons(request);
		return buluResult;
	}

	// 使用优惠卷
	@RequestMapping("/useCoupons")
	@ResponseBody
	public BuluResult useCoupons(HttpServletRequest request, Integer couponsId) {
		BuluResult buluResult = customerService.useCoupons(request, couponsId);
		return buluResult;
	}

	// 设置里面的操作指南,关于我们,版本介绍
	@RequestMapping("/setting")
	@ResponseBody
	public BuluResult setting() {
		BuluResult buluResult = customerService.setting();
		return buluResult;
	}

	// 点击进行中
	@RequestMapping("/clickGoing")
	@ResponseBody
	public BuluResult clickGoing(HttpServletRequest request, int types) {
		BuluResult buluResult = customerService.clickGoing(request, types);
		return buluResult;
	}

	// @RequestMapping("/clickPaying")
	// @ResponseBody
	// public BuluResult clickPaying(HttpServletRequest request) {
	// System.out.println("&&&&&&&&&&&&&&&&&&&");
	// BuluResult buluResult = customerService.clickPaying(request);
	// return buluResult;
	// }

	// 进行中,未安装,已取消一个订单中,展示所有的商品(已取消中的二级菜单也是这个接口)
	@RequestMapping("/getGoodsByIndentId")
	@ResponseBody
	public BuluResult getGoodsByIndentId(HttpServletRequest request, HttpServletResponse response, Integer indentId) {
		BuluResult buluResult = customerService.getGoodsByIndentId(request, indentId);
		return buluResult;
	}

	// 进行中如果订单中都是未测量的商品可以取消, 并保留在已取消中
	@RequestMapping("updateIndentByIndentId")
	@ResponseBody
	public BuluResult updateIndentByIndentId(HttpServletRequest request, Integer indentId) {
		BuluResult buluResult = customerService.updateIndentByIndentId(request, indentId);
		return buluResult;
	}

	// 进行中一个订单中的商品 顾客选的配件可以删除
	@RequestMapping("/deleteAccOfGoodOfIndent")
	@ResponseBody // goodsId这个是商品的id,// accId该商品配件的id
	public BuluResult deleteAccOfGoodOfIndent(HttpServletRequest request, Integer goodsId, Integer accId) {
		BuluResult buluResult = customerService.deleteAccOfGoodOfIndent(request, goodsId, accId);
		return buluResult;
	}

	// 点击重新购买!(把已取消中的订单改成进行中)
	@RequestMapping("reBuyIndent")
	@ResponseBody
	public BuluResult reBuyIndent(HttpServletRequest request, Integer indentId) {
		BuluResult buluResult = customerService.reBuyIndent(request, indentId);
		return buluResult;
	}

	// 运行中的重选款式:把原来订单中的商品改成,重新购买后的商品
	@RequestMapping("/updateGoodOfIndent")
	@ResponseBody
	public BuluResult updateGoodOfIndent(HttpServletRequest request, String goodsId, String sampleId) {
		BuluResult buluResult = customerService.updateGoodOfIndent(request, goodsId, sampleId);
		return buluResult;
	}

	// 运行中, 测量完成. 开始付测量费
	@RequestMapping("/payMeasureMoney")
	@ResponseBody
	public BuluResult payMeasureMoney(HttpServletRequest request, Integer indentId) {
		BuluResult buluResult = customerService.payMeasureMoney(request, indentId);
		return buluResult;
	}

	// 点击已取消
	@RequestMapping("/clickCancel")
	@ResponseBody
	public BuluResult clickCancel(HttpServletRequest request) {
		BuluResult buluResult = customerService.clickCancel(request);
		return buluResult;
	}

	// (购物车)进行中点击是否安装之后 刚开始所有的indent,还是没有付钱的(只是付了一个定金), 当测量完成之后顾客点击确认付款
	// ,可以让顾客选择是否安装
	@RequestMapping("/indentToOrders") // goods是全部商品
	@ResponseBody // {goods:[{goodsId:,sampleDescribe:,goodsPrice,isUse:0是没勾选,1勾选}],cuAdId:,countPrice:,isAZ:0不装,indentPrice:安装费用////
					// }
	public BuluResult indentToOrders(HttpServletRequest request, String good) {
		BuluResult buluResult = customerService.indentToOrders(request, good);
		return buluResult;
	}

	// 进入待评价页面
	// @RequestMapping("/NoEvaluation")
	// @ResponseBody
	// public BuluResult NoEvaluation(HttpServletRequest request) {
	// BuluResult buluResult = customerService.NoEvaluation(request);
	// return buluResult;
	// }

	// 待评价页面点击某个订单
	@RequestMapping("/GoEvaluation")
	@ResponseBody
	public BuluResult GoEvaluation(Integer indentId) {
		BuluResult buluResult = customerService.GoEvaluation(indentId);
		return buluResult;
	}

	/**
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/submitEvaluation")
	@ResponseBody
	public BuluResult submitEvaluation(HttpServletRequest request) {
		Integer type = Integer
				.parseInt(request.getParameter("type") == null ? "0" : request.getParameter("type").toString());// 评价对象
		String content = request.getParameter("content");// 评价内容
		String imgs = request.getParameter("imgs");// 评价图片
		String sampleId = request.getParameter("sampleId");// 评价产品
		String goodsId = request.getParameter("goodsId");// 评价产品
		String star = request.getParameter("star");// 评价星级
		String workerId = request.getParameter("workerId");// 工人
		String indentId = request.getParameter("indentId");// 订单

		BuluResult buluResult = new BuluResult();
		if (indentId == null || "".equals(indentId)) {
			buluResult.setStatus(1);
			buluResult.setMessage("参数不为空");
			return buluResult;
		} else {
			if (type == 0) {// 商品评价
				if (sampleId == null || "".equals(sampleId) || goodsId == null || "".equals(goodsId)) {
					buluResult.setStatus(1);
					buluResult.setMessage("参数不为空");
					return buluResult;
				}
			} else {// 工人评价
				if (workerId == null || "".equals(workerId)) {
					buluResult.setStatus(1);
					buluResult.setMessage("参数不为空");
					return buluResult;
				}
			}
		}
		Evaluation evaluation = new Evaluation();
		evaluation.setContent(content);
		evaluation.setIndentId(Integer.parseInt(indentId));
		evaluation.setStar(star == null || "".equals(star) ? null : Integer.parseInt(star));
		evaluation.setWorkerId(workerId == null || "".equals(workerId) ? null : Integer.parseInt(workerId));
		evaluation.setSampleId(sampleId == null || "".equals(sampleId) ? null : Integer.parseInt(sampleId));
		evaluation.setType(type);
		return customerService.submitEvaluation(request, evaluation, imgs, goodsId);
	}

	// 用户订单
	@RequestMapping("/successfulOrders")
	@ResponseBody
	public BuluResult successfulOrders(HttpServletRequest request) {
		BuluResult buluResult = customerService.clickOver(request);
		return buluResult;
	}

	// 已付款
	// 点击已付款
	@RequestMapping("/clickAlreadyPay")
	@ResponseBody
	public BuluResult clickAlreadyPay(HttpServletRequest request) {
		BuluResult buluResult = customerService.clickAlreadyPay(request);
		return buluResult;
	}

	// 根据订单号显示所有他的商品以及商品的信息
	@RequestMapping("/getGoodsByOrderId")
	@ResponseBody
	public BuluResult getGoodsByOrdersId(Integer indentId) {
		BuluResult buluResult = customerService.getGoodsByOrderId(indentId);
		return buluResult;
	}

	// 已付款中, 点击确认收货, 弹出两个提示, 是否选择安装,选择需要安装的话, 前台提供安装时间
	// 首先确认收货
	// @RequestMapping("/confirmReceipt")
	@RequestMapping("/insertAzhuang")
	@ResponseBody
	public BuluResult confirmReceipt(HttpServletRequest request) {
		BuluResult buluResult = customerService.confirmReceipt(request, request.getParameter("goodsList"));
		return buluResult;
	}

	// 确认收货之后生成安装
	@RequestMapping("/insertInstallIndent")
	@ResponseBody
	public BuluResult insertInstallIndent(HttpServletRequest request, Integer orderId, Date date, Integer cuAdId,
			Integer isAZ) {
		BuluResult buluResult = customerService.insertInstallIndent(request, orderId, date, cuAdId, isAZ);
		return buluResult;
	}

	// 查询客户信息
	@RequestMapping("/selectCustomerX")
	@ResponseBody
	public BuluResult selectCustomerX(HttpServletRequest request) {
		BuluResult buluResult = customerService.selectCustomerX(request);
		return buluResult;
	}

	// 点击待安装
	@RequestMapping("/clickNoInstall")
	@ResponseBody
	public BuluResult clickNoInstall(HttpServletRequest request) {
		BuluResult buluResult = customerService.clickNoInstall(request);
		return buluResult;
	}

	// 待安装中点击安装完成
	@RequestMapping("/installComplete")
	@ResponseBody
	public BuluResult installComplete(Integer indentId) {
		BuluResult buluResult = customerService.installComplete(indentId);
		return buluResult;
	}

	// 编辑地址接口
	@RequestMapping("/chageAddress")
	@ResponseBody
	public BuluResult chageAddress(Customer_address customer_address) {
		BuluResult buluResult = customerService.chageAddress(customer_address);
		return buluResult;
	}

	// 进入我的页面
	@RequestMapping("/Mypage")
	@ResponseBody
	public BuluResult Mypage(HttpServletRequest request) {
		BuluResult buluResult = customerService.Mypage(request);
		return buluResult;
	}

	// 申请售后
	@RequestMapping("/insertAfterSales")
	@ResponseBody
	public BuluResult insertAfterSales(HttpServletRequest request, Integer goodsId, String problem, String imgs) {
		BuluResult buluResult = customerService.insertAfterSales(request, goodsId, problem, imgs);
		return buluResult;
	}

	// 查看售后状态
	@RequestMapping("/selectAfterSales")
	@ResponseBody
	public BuluResult selectAfterSales(Integer goodsId) {
		BuluResult buluResult = customerService.selectAfterSales(goodsId);
		return buluResult;
	}

	// 搜索配件
	@RequestMapping("/selectPeijian")
	@ResponseBody
	public BuluResult selectPeijian(Integer accessoriesTypeId, Integer page) {
		BuluResult buluResult = customerService.selectPeijian(accessoriesTypeId, page);
		return buluResult;
	}

	// 修改配件米数数量
	@RequestMapping("/updatePeijian")
	@ResponseBody
	public BuluResult updatePeijian(String peijian) {
		BuluResult buluResult = customerService.updatePeijian(peijian);
		return buluResult;
	}

	// 问题反馈
	@RequestMapping("/insertProblem")
	@ResponseBody
	public BuluResult insertProblem(HttpServletRequest request, String problemCount, String imgs) {
		BuluResult buluResult = customerService.insertProblem(request, problemCount, imgs);
		return buluResult;
	}

	// 顾客应支付的定金
	@RequestMapping("/selectDingjing")
	@ResponseBody
	public BuluResult selectDingjing(String city, HttpServletResponse response) {
		BuluResult buluResult = customerService.selectDingjing(city);
		return buluResult;
	}

	// TODO 查看物流
	// 查询工人信息
	@RequestMapping("/selectWorkerX")
	@ResponseBody
	public BuluResult selectWorkerX(Integer indentId) {
		BuluResult buluResult = customerService.selectWorkerX(indentId);
		return buluResult;
	}

	// 根据是什么配件查询可用什么类型的配件
	@RequestMapping("/selectLihaibu")
	@ResponseBody
	public BuluResult selectLihaibu(Integer whatStructure, Integer accessoriesTypeId, Integer page) {
		BuluResult buluResult = customerService.selectLihaibu(whatStructure, accessoriesTypeId, page);
		return buluResult;
	}

	// 查询顾客钱包
	@RequestMapping("/selectWallet")
	@ResponseBody
	public BuluResult selectWallet(HttpServletRequest request) {
		BuluResult buluResult = customerService.selectWallet(request);
		return buluResult;
	}

	// 点击提现查询绑定情况
	@RequestMapping("/selectBangding")
	@ResponseBody
	public BuluResult selectBangding(HttpServletRequest request) {
		BuluResult buluResult = customerService.selectBangding(request);
		return buluResult;
	}

	// 查询顾客金钱明细
	@RequestMapping("/selectMingxi")
	@ResponseBody
	public BuluResult selectMingxi(HttpServletRequest request, Integer page) {
		BuluResult buluResult = customerService.selectMingxi(request, page);
		return buluResult;
	}

	// 添加提现银行卡
	@RequestMapping("/insertYinhangka")
	@ResponseBody
	public BuluResult insertYinhangka(HttpServletRequest request, String bankName, String boundBank,
			String bankNumber) {
		BuluResult buluResult = customerService.insertYinhangka(request, bankName, boundBank, bankNumber);
		return buluResult;
	}

	// 添加支付宝
	@RequestMapping("/insertZhifubao")
	@ResponseBody
	public BuluResult insertZhifubao(HttpServletRequest request, String baoName, String zhifubao) {
		BuluResult buluResult = customerService.insertZhifubao(request, baoName, zhifubao);
		return buluResult;
	}

	// 添加微信
	@RequestMapping("/insertWeixin")
	@ResponseBody
	public BuluResult insertWeixin(HttpServletRequest request, String xinName, String weixin) {
		BuluResult buluResult = customerService.insertWeixin(request, xinName, weixin);
		return buluResult;
	}

	// 已完成中生成测量订单
	// @RequestMapping("/insertAzhuang")
	// @ResponseBody
	// public BuluResult insertAzhuang(HttpServletRequest request, String
	// goodIds) {
	// BuluResult buluResult = customerService.insertAzhuang(request, goodIds);
	// return buluResult;
	// }

	@RequestMapping("/cancelIndent")
	@ResponseBody
	public BuluResult cancelIndent(HttpServletRequest request, Integer indentId, Integer type) {
		return customerService.cancelIndent(request, indentId, type);
	}

	@RequestMapping("/cancleOrders")
	@ResponseBody
	public BuluResult cancleOrders(HttpServletRequest request, Integer orderId) {
		BuluResult buluResult = customerService.cancelOrders(request, orderId);
		return buluResult;
	}

	// 查询物流
	@RequestMapping("/selectWuliu")
	@ResponseBody
	public BuluResult selectWuliu(Integer orderId) {
		BuluResult buluResult = customerService.selectWuliu(orderId);
		return buluResult;
	}

	// 查询广告
	@RequestMapping("/selectGuanggao")
	@ResponseBody
	public BuluResult selectGuanggao() {
		BuluResult buluResult = customerService.selectGuanggao();
		return buluResult;
	}

	// goodsList {goods:[{goodsId:1...},{...},{...},{}],allFee:,earnest:200,
	// appointmentTime:2017-6-8 14:30,measurement:0 }
	@RequestMapping("/banlancePay")
	@ResponseBody
	public BuluResult banlancePayment(HttpServletRequest request) {
		Map<String, String> paras = new HashMap<String, String>();
		paras.put("money", request.getParameter("money"));
		paras.put("indentId", request.getParameter("indentId"));
		paras.put("measurement", request.getParameter("measurement"));
		paras.put("account", request.getParameter("account"));
		// 行为交易种类1:定金，2:安装费，3:尾款，4:充值,5:提现6:全配件支付,',
		paras.put("type", request.getParameter("type"));
		return customerService.banlancePayment(request, paras);
	}

	@RequestMapping("/submitIndent")
	@ResponseBody
	public BuluResult submitIndent(HttpServletRequest request) {
		return customerService.insertIndent(request, request.getParameter("goodsList"));
	}

	// 请求定金
	@RequestMapping("/requestEarnest")
	@ResponseBody
	public BuluResult requestEarnest(HttpServletRequest request) {
		return customerService.requestEarnest(request, request.getParameter("curtainNum"),
				request.getParameter("city"));
	}

	// 点击商品查询商品信息
	@RequestMapping("/selectGoodxinxi")
	@ResponseBody
	public BuluResult selectGoodxinxi(Integer goodsId) {
		BuluResult buluResult = customerService.selectGoodxinxi(goodsId);
		return buluResult;
	}

	/**
	 * 
	 * @param request
	 * @param indentId
	 *            :订单号
	 * @param money:支付金额
	 * @param type:1:定金，2:安装费，3:尾款，4:充值,5:提现6:全配件支付,',
	 * @return
	 */
	@RequestMapping("/alipayReq")
	@ResponseBody
	public BuluResult aliPayReq(HttpServletRequest request, Integer indentId, Double money, Integer type) {
		return customerService.alipayMoney(request, indentId, money, type);
	}

	// 提现
	@RequestMapping("/alipyWithdrawal")
	@ResponseBody
	public BuluResult alipyWithdrawal(HttpServletRequest request, Double money, String zhifubao, String name,
			String account, Integer status) {
		BuluResult buluResult = customerService.alipyWithdrawal(request, money, zhifubao, name, account, status);
		return buluResult;
	}

	// 充值
	@RequestMapping("/recharge")
	@ResponseBody
	public BuluResult recharge(HttpServletRequest request, Double money, Integer method, String alipayAccount) {
		BuluResult buluResult = customerService.recharge(request, money, method, alipayAccount);
		return buluResult;
	}

	// 点击商品查询商品信息
	@RequestMapping("/addSampleParam")
	@ResponseBody
	public Object addSampleParam(HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
		response.setHeader("Access-Control-Max-Age", "3600");
		response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
		BuluResult buluResult = customerService.addSampleParam();
		return buluResult.getData();
	}

	// 商品信息的添加
	@RequestMapping("/insert_Sample")
	@ResponseBody
	public BuluResult insert_Sample() {
		// BuluResult buluResult=customerService
		return null;
	}

	// 字典新属性的添加
	@RequestMapping("/dic_insert")
	@ResponseBody
	public BuluResult dic_insert(HttpServletRequest request) {
		Dictionary dictionary = new Dictionary();
		dictionary.setDicName(request.getParameter("dicName"));
		dictionary.setDicValue(request.getParameter("dicValue"));
		dictionary.setMemo(request.getParameter("memo"));
		dictionary.setDicNum(Integer.parseInt(request.getParameter("dicNum")));
		BuluResult buluResult = customerService.dic_insert(dictionary);
		return buluResult;
	}

	// 查询字典表细分参数数据
	@RequestMapping("/selectDictionarybyParam")
	@ResponseBody
	public BuluResult selectDictionarybyParam(HttpServletRequest request, HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
		response.setHeader("Access-Control-Max-Age", "3600");
		response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
		Map<String, Object> map = new HashMap<>();
		map.put("dic_name", request.getParameter("dic_name"));
		map.put("dic_num", Integer.parseInt(request.getParameter("dic_num")));
		return customerService.selectDictionarybyParam(map);
	}

	/**
	 * 
	 * @param request
	 * @param structure
	 * @param isStyle
	 * @param type
	 * @return
	 */
	@RequestMapping("/selectStyles")
	@ResponseBody
	public BuluResult selectSamplesOfStyle(HttpServletRequest request, Integer structure, Integer isStyle, Integer type,
			Integer page) {
		Map<String, Object> parames = new HashMap<String, Object>();
		parames.put("structure", structure);
		parames.put("isSty", isStyle);
		parames.put("type", type);
		page = (page == null ? 0 : (page - 1) * 10);
		parames.put("page", page);
		return customerService.selectSamplesOfStyles(request, parames);
	}

	/**
	 * 重选款式
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/reselect")
	@ResponseBody
	public BuluResult reselectStyle(HttpServletRequest request, Integer indentId, Integer goodsId, Integer sampleId) {
		BuluResult buluResult = new BuluResult();
		if (indentId == null | goodsId == null | sampleId == null) {
			buluResult.setStatus(1);
			buluResult.setMessage("参数不能为空");
			return buluResult;
		}
		Map<String, Object> parames = new HashMap<String, Object>();
		parames.put("indentId", indentId);
		parames.put("goodsId", goodsId);
		parames.put("sampleId", sampleId);
		buluResult = customerService.reselectStyle(request, parames);
		return buluResult;
	}

	@RequestMapping("myCode")
	public String myCode(HttpServletRequest request, HttpServletResponse response, Integer customerId) {
		Customer  customer=customerService.getCustomerById(customerId);
		//Customer  customer=customerService.getCurrentLoggingCustomer(request);
		request.setAttribute("customerId", customerId);
		request.setAttribute("vertification", customer !=null?customer.getVertification():"");
		return "/jsp/code";
	}

	/**
	 * 根据URL生成二维码
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping("/getQRCode")
	public void getQRCode(HttpServletRequest request, HttpServletResponse response, String headImg, Integer identity,
			Integer customerId) {
		// 二维码图片输出流
		OutputStream out = null;
		try {
			String url =null;
			if(customerId ==null)return;//如果用户未登录，返回
			if(identity==4){
				url="http://fir.im/lukejiaju"; //跳转到鹿客家居app 下载页面	
			}else{
				url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/"
						+ request.getContextPath() + "/customer/reg?customerId=" + customerId + "&identity=" + identity;
			}
			System.out.println(url);
			response.setContentType("image/jpeg;charset=UTF-8");
			BufferedImage image = QRCodeUtil.createQRCode(url);
			// 实例化输出流对象
			out = response.getOutputStream();
			// 画图
			ImageIO.write(image, "png", response.getOutputStream());
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (null != response.getOutputStream()) {
					response.getOutputStream().close();
				}
				if (null != out) {
					out.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 二维码扫描注册页面
	 * 
	 * @param request
	 * @param customerId
	 * @param identity
	 * @return
	 */
	@RequestMapping("/reg")
	public String toReg(HttpServletRequest request, Integer customerId, Integer identity) {
		// Customer customer=customerService.getCustomerById(customerId);
		// Map<String,Object>map=new HashMap<>();
		// map.put("customer", customer);
		// map.put("identity", identity);
		// BuluResult bulu=new BuluResult();
		// bulu.setStatus(0);
		// bulu.setData(map);
		request.setAttribute("identity", identity);
		request.setAttribute("customerId", customerId);
		if (identity == 0) {  //顾客扫码注册
			return "/jsp/reg";
		} else {  //经销商和推广员扫码注册
			return "/jsp/regist";
		}
	}

	/**
	 * 注册成经销商或推广员
	 * 
	 * @param request
	 * @param session
	 * @param phone
	 * @param vertifyCode
	 * @return
	 */
	@RequestMapping("/subordinate")
	@ResponseBody
	public BuluResult subordinate(HttpServletRequest request, HttpServletResponse response, HttpSession session, String phone, String vertifyCode,
			Integer customerId, String passwd, String nickName,Integer identity) {
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
		response.setHeader("Access-Control-Max-Age", "3600");
		response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
		BuluResult bulu = checkCustomerInfo(phone, vertifyCode, customerId);
		if (bulu.getStatus() == 0) {
			// 如果account有值
			if (passwd != null  &&  !("".trim().equals(passwd))) {
				// 先注册
				try {
					BuluResult br = customerService.registWeb(session, nickName, phone, passwd,
							customerService.getCustomerById(customerId).getPhone(), null,identity);
					if (br.getStatus() == 0) {
						// 注册成功，绑定关系
						return customerService.subordinate(request, session, phone, vertifyCode, customerId,identity);
					} else {
						return br;
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			// 已经注册过的，绑定关系
			return customerService.subordinate(request, session, phone, vertifyCode, customerId,identity);
		}
		return bulu;
	}

	/**
	 * 顾客扫描注册并跳转到鹿客家居app下载页面
	 * 
	 * @param request
	 * @param session
	 * @param phone
	 * @param vertifyCode
	 * @return
	 */
	@RequestMapping("customerRefer")
	@ResponseBody
	public BuluResult customerRefer(HttpServletRequest request, HttpSession session, String phone, String vertifyCode,
			Integer customerId) {
		BuluResult bulu = checkCustomerInfo(phone, vertifyCode, customerId);
		if (bulu.getStatus() == 0) {
			return customerService.recordUserAndCustomer(request, session, phone, vertifyCode, customerId);
		}
		return bulu;
	}

	/**
	 * 功能：判断手机号是否注册
	 * 
	 * @param phone
	 * @return
	 */
	@RequestMapping("isRegisted")
	@ResponseBody
	public BuluResult isRegisted(String phone) {
		return customerService.isReigister(phone);
	}

	/**
	 * 手机号格式验证 验证码格式验证
	 * 
	 * @param phone
	 * @param vertifyCode
	 * @return
	 */
	public BuluResult checkCustomerInfo(String phone, String vertifyCode, Integer customerId) {
		String regex = "1(3|5|7|8)[0-9]{9}";
		BuluResult bulu = new BuluResult();
		if (customerId == null) {
			bulu.setStatus(1);
			return bulu;
		}
		if (phone == null || !(phone.trim().matches(regex))) {
			bulu.setMessage("手机号码输入错误");
			bulu.setStatus(1);
			return bulu;
		}
		if (vertifyCode == null || ("".equals(vertifyCode))) {
			bulu.setMessage("验证码输入错误");
			bulu.setStatus(1);
			return bulu;
		}
		bulu.setMessage("格式验证通过");
		bulu.setStatus(0);
		return bulu;
	}

}
